import { baseUrl, bankCard } from '../../../../utils/config'
const moment = require('../../../../utils/moment.js')
const { get } = require('../../../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    birthday: '0000年00月00日',
    baseUrl: baseUrl,
    childInf: {
      memberName: "",
      sex: 0,
      headPicPath: "",
      birthday: "2015-01-01"
    },
    headImagePath: "user.png",
    cardName: '',
    sexIndex: 0,
    sexArray: ['男', '女'],
    currentIndex: 0,
    cardOptions: bankCard,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad({ id }) {
    if (id) {
      get(`family/get/${id}`,
        { "joinDomain": "bankAccount" },
        (data) => {
          data.birthday = moment(data.birthdayStr, 'YYYY年MM月DD日').format('YYYY-MM-DD');
          let currentIndex = 0;
          this.data.cardOptions.forEach((item, index) => {
            if (item.cardName == data.bankAccount.cardName) {
              currentIndex = index;
            }
          })

          this.setData({
            childInf: data,
            birthday: data.birthdayStr,
            headImagePath: data.headPicPath,
            currentIndex: currentIndex,
            sexIndex: data.sex == '男' ? 0 : 1
          })

        })
    } else {
      this.setData({
        cardName: this.data.cardOptions[0].cardName
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  bindSexChange({ detail: { value } }) {
    this.setData({
      sexIndex: value
    })
  },
  bindDateChange({ detail: { value } }) {
    const formateDate = moment(value).format('YYYY年MM月DD日');
    this.setData({
      birthday: formateDate
    })
  },
  cancel() {
    wx.navigateBack();
  },
  confirm() {
 
    if (this.data.headImagePath == 'user.png') {
      wx.showToast({
        title: '请选择头像',
        icon: 'error',
        duration: 2000
      });
      return;
    }
    if (this.data.childInf.memberName == '') {
      wx.showToast({
        title: '请输入姓名',
        icon: 'error',
        duration: 2000
      });
      return;
    }

    if (this.data.birthday == '0000年00月00日') {
      wx.showToast({
        title: '请填写出生日期',
        icon: 'error',
        duration: 2000
      });
      return;
    }

    const bankCard = this.data.cardOptions[this.data.currentIndex];

    if (this.data.childInf.bankAccount) {
      bankCard.id = this.data.childInf.bankAccount.id
    }

    const params = {
      memberAlias: "小孩",
      memberName: this.data.childInf.memberName,
      sex: this.data.sexArray[this.data.sexIndex],
      birthdayStr: this.data.childInf.birthday,
      headPicPath: this.data.headImagePath,
      unicode: wx.getStorageSync('unicode'),
      "bankAccount.cardNumber": bankCard.cardNumber,
      "bankAccount.cardName": bankCard.cardName,
      "bankAccount.cardImg": bankCard.cardImg
    }

    if (bankCard.id) {
      params["bankAccount.id"] = bankCard.id;
    }

    if (this.data.childInf.id) {
      params["id"] = this.data.childInf.id
    }


    get('family/save', params, () => {
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 2000,
        success: function () {
          setTimeout(() => {
            const pages = getCurrentPages();
            const familyPage = pages[pages.length - 2];
            familyPage.freshChild();
            wx.navigateBack();

            const settingPage = pages[pages.length - 3];
            if (settingPage.checkFamilyInfo) {
              settingPage.checkFamilyInfo()
            }
          }, 2000);
        }
      });
    })

  },
  chooseHeadPic() {
    wx.navigateTo({
      url: '/pages/me/family/pickHeadImg/pickHeadImg?headType=child',
    })
  },
  onChangeCard({ detail: { current, source } }) {
    this.setData({
      currentIndex: current,
      cardName: this.data.cardOptions[current].cardName
    })
  }
})